package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.vo.UserAddress;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;

public class UserAddressJdbc extends BaseJdbc {

    Logger logger = LoggerFactory.getLogger(getClass());

    public static final String USERADDRESS_INSERT = "insert into user_address (userId, recipientName, streetAddress, zipCode, phoneNumber, mobileNumber, province, city, area)values(?,?,?,?,?,?,?,?,?)";

    public static final String USERADDRESS_UPDATE = "UPDATE user_address set recipientName = ? , streetAddress = ? , zipCode = ? , phoneNumber = ? , mobileNumber = ? , province = ? , city = ? , AREA = ? WHERE id = ? AND userId = ?";

    public static final String USERADDRESS_GETBYID = "select * from  user_address u where u.id = ?";

    public static final String USERADDRESS_GETBYUSERID= "select * from  user_address u where u.userId = ?";



    public void save(UserAddress userAddress) {
        Connection conn = this.getConnection();
        Object[] pram = {userAddress.getUserId(), userAddress.getRecipientName(), userAddress.getStreetAddress(), userAddress.getZipCode(), userAddress.getPhoneNumber(), userAddress.getMobileNumber(), userAddress.getProvince(), userAddress.getCity(), userAddress.getArea()};
        try {
            this.execute(conn, USERADDRESS_INSERT, pram);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
    }



    public void update(UserAddress userAddress) {
        Connection conn = this.getConnection();
        Object[] pram = {userAddress.getRecipientName(), userAddress.getStreetAddress(), userAddress.getZipCode(), userAddress.getPhoneNumber(), userAddress.getMobileNumber(), userAddress.getProvince(), userAddress.getCity(), userAddress.getArea(), userAddress.getId(), userAddress.getUserId()};
        try {
            this.execute(conn, USERADDRESS_UPDATE, pram);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
    }



    public UserAddress getById(int id) {
        Connection conn = this.getConnection();
        UserAddress userAddress = null;
        try {
            userAddress = this.query(conn, USERADDRESS_GETBYID, new BeanHandler<UserAddress>(UserAddress.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
		}
		return userAddress;
	}


    public UserAddress getByUserId(int userId) {
        Connection conn = this.getConnection();
        UserAddress userAddress = null;
        try {
            userAddress = this.query(conn, USERADDRESS_GETBYUSERID, new BeanHandler<UserAddress>(UserAddress.class), userId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
		}
		return userAddress;
	}
}
